//
//  HMNewfeatureViewController.m
//
//  Created by apple on 14-7-7.
//  Copyright (c) 2014年 . All rights reserved.
//

#define HMNewfeatureImageCount 3

#import "HMNewfeatureViewController.h"
#import "BaseTabbarVC.h"
#import "UIView+CYExt.h"
#import "LoginVC.h"

@interface HMNewfeatureViewController () <UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *pageControl;
@end

@implementation HMNewfeatureViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.添加UISrollView
    [self setupScrollView];
    
    // 2.添加pageControl
    [self setupPageControl];
}

/**
 *  添加UISrollView
 */
- (void)setupScrollView
{
    // 1.添加UISrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    // 2.添加图片
    CGFloat imageW = scrollView.width;
    CGFloat imageH = scrollView.height;
    for (int i = 0; i<HMNewfeatureImageCount; i++) {
        // 创建UIImageView
        UIImageView *imageView = [[UIImageView alloc] init];
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        
        // 设置frame
        imageView.y = 0;
        imageView.width = imageW;
        imageView.height = imageH;
        imageView.x = i * imageW;
        
        [self setupImageView:imageView];
        
                 //给最后一个imageView添加按钮
                if (i == HMNewfeatureImageCount - 1) {
                    [self setupLastImageView:imageView];
                }else{
                    [self setupImageView:imageView];
                }
    }
    
    // 3.设置其他属性
    scrollView.contentSize = CGSizeMake(HMNewfeatureImageCount * imageW, 0);
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
    //别在scrollView.subviews中通过索引来查找对应的子控件
    //    [scrollView.subviews lastObject];
}

/**
 *  添加pageControl
 */
- (void)setupPageControl
{
    // 1.添加
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = HMNewfeatureImageCount;
    pageControl.centerX = self.view.width * 0.5;
    pageControl.centerY = self.view.height - 30;
    [self.view addSubview:pageControl];
    
    // 2.设置圆点的颜色
    pageControl.currentPageIndicatorTintColor = [UIColor blueColor]; // 当前页的小圆点颜色
    pageControl.pageIndicatorTintColor = [UIColor colorWithHexString:@"#CCCCCC"]; // 非当前页的小圆点颜色
    self.pageControl = pageControl;
}

/**
 设置最后一个UIImageView中的内容
 */
- (void)setupLastImageView:(UIImageView *)imageView
{
    imageView.userInteractionEnabled = YES;
    
    // 添加开始按钮
    [self setupStartButton:imageView];
}
- (void)setupImageView:(UIImageView *)imageView
{
    // 添加跳过按钮
    imageView.userInteractionEnabled = YES;
    [self setupShareButton:imageView];
}

/**
 *  添加跳过按钮
 */
- (void)setupShareButton:(UIImageView *)imageView
{
    // 1.添加按钮
    UIButton *shareButton = [[UIButton alloc] init];
    [imageView addSubview:shareButton];
    // 2.设置文字和图标
    [shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [shareButton setBackgroundImage:[UIImage imageNamed:@"new_feature_jump_button"] forState:UIControlStateNormal];
    [shareButton setBackgroundImage:[UIImage imageNamed:@"new_feature_jump_button"] forState:UIControlStateHighlighted];
    // 监听点击
    [shareButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    
    // 3.设置frame
    shareButton.size = shareButton.currentBackgroundImage.size;
    shareButton.y = self.view.height * 0.08;
    shareButton.x = self.view.width*0.85;
}

/**
 *  添加开始按钮
 */
- (void)setupStartButton:(UIImageView *)imageView
{
    // 1.添加开始按钮
    UIButton *startButton = [[UIButton alloc] init];
    [imageView addSubview:startButton];
    
    // 2.设置背景图片
    [startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_start_button"] forState:UIControlStateNormal];
    [startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_start_button"] forState:UIControlStateHighlighted];
    
    // 3.设置frame
    startButton.size = startButton.currentBackgroundImage.size;
    startButton.centerX = self.view.width * 0.5;
    startButton.centerY = self.view.height - 190;
    
    
    // 4.设置文字
    [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
}

/**
 *  开始
 */
- (void)start
{
    [UIApplication sharedApplication].statusBarHidden = NO;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    NSUDRemoveValueWithKey(@"Token");
    LoginVC *login = [[LoginVC alloc]initWithNibName:@"LoginVC" bundle:nil];
    BaseNavVC *navLogin = [[BaseNavVC alloc] initWithRootViewController:login];
//    login.NeedChangeWindow = YES;
    window.rootViewController = navLogin;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 获得页码
    CGFloat doublePage = scrollView.contentOffset.x / scrollView.width;
    int intPage = (int)(doublePage + 0.5);
    // 设置页码
    self.pageControl.currentPage = intPage;
    
    //最后一页某一位置跳转
    if(doublePage>2.02){
        [self start];
    }
}

@end
